//
//  ZWHSearchTableViewCell.swift
//  约麻
//
//  Created by Syrena on 2017/7/20.
//  Copyright © 2017年 Sweet. All rights reserved.
//

import UIKit
import AVOSCloud
import Hyphenate
import MBProgressHUD

protocol ZWHSearchTableViewCellDelegate {
    func requestfriend(username:String!)
}

class ZWHSearchTableViewCell: UITableViewCell {
    @IBOutlet weak var iconV: UIImageView!

    @IBOutlet weak var nameL: UILabel!
    
    @IBOutlet weak var addB: UIButton!
    
    var delegate:ZWHSearchTableViewCellDelegate?
    
    var user:AVUser?{
        didSet{
            if let name = user?.object(forKey: "name") as? String{
                nameL.text = name
            }else{
                nameL.text = user?.username
            }
            
            if let image = user?.object(forKey: "headimage") as? String{
                iconV.sd_setImage(with: URL(string: image))
            }else{
                iconV.image = UIImage(named: "茶叶")
            }
            
            var error:EMError?
            let array = EMClient.shared().contactManager.getContactsFromServerWithError(&error)
            let userlist = array as? [String]
            for myuser in userlist!{
                if user?.username == myuser{
                    addB.isEnabled = false
                    addB.setTitle("已添加", for: .normal)
                    addB.backgroundColor = #colorLiteral(red: 0.8039215803, green: 0.8039215803, blue: 0.8039215803, alpha: 1)
                }
            }
            
            
            addB.layer.cornerRadius = 5
            addB.layer.masksToBounds = true
        }
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
    @IBAction func addfriend(_ sender: UIButton) {
        delegate?.requestfriend(username: user?.username)
    }
}
